package it.kion.util.ui.ulog2.popup.actions;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.*;
import org.eclipse.ui.*;
import it.kion.util.ui.ulog2.Log4jUtil;

public class AddLoggerAction implements IObjectActionDelegate
{
   private ICompilationUnit compilationUnit;

   public AddLoggerAction()
   {
      super();
   }

   public void setActivePart(
      IAction action,
      IWorkbenchPart targetPart)
   {
   }

   public void run(IAction action)
   {
      Log4jUtil.addLogger(compilationUnit);
   }

   private ICompilationUnit getCompilationUnit(ISelection selection)
   {
      if (selection != null
         && selection instanceof IStructuredSelection)
      {
         IStructuredSelection structuredSelection =
            (IStructuredSelection) selection;
         Object firstElement =
            structuredSelection.getFirstElement();
         if (firstElement != null
            && firstElement instanceof ICompilationUnit)
         {
            return (ICompilationUnit) firstElement;
         }
      }
      return null;
   }

   public void selectionChanged(
      IAction action,
      ISelection selection)
   {
      this.compilationUnit = getCompilationUnit(selection);
      action.setEnabled(
         compilationUnit != null
            && !Log4jUtil.hasLogger(compilationUnit));
   }
}
